﻿using MeetAI.Model;
using MeetAI.Model.DTO;
using MeetAI.Model.VO;
using MeetAI.Repository.IRepository;
using MeetAI.Utility.Table;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MeetAI.Repository
{
    public class Dy_Article_Repository : BaseRepository<dy_article>, IDy_Article_Repository
    {
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="pageIndex">当前页面索引</param>
        /// <param name="pageSize">分布大小</param>
        /// <returns></returns>
        public async Task<IPagedList<dy_article_index_vo>> getArticlePageList(Expression<Func<dy_article, bool>> predicate, string orderBy = "", int pageIndex = 1, int pageSize = 20)
        {
            using (var db = DbFactory.GetSqlSugarClient())
            {
                RefAsync<int> totalCount = 0;
                var page = await db.Queryable<dy_article, dy_article_field>((f, t2) => new object[] { JoinType.Inner, f.id == t2.aid })
                    .Where(predicate)
                    .Select((f, t2) => new dy_article_index_vo
                    {
                        title = f.title,
                        time = f.addtime,
                        litpic = f.litpic,
                        videoUrl = t2.videourl
                    })
                    .OrderBy(orderBy)
                    .ToPageListAsync(pageIndex, pageSize, totalCount);
                var list = new PagedList<dy_article_index_vo>(page, pageIndex, pageSize, totalCount);
                return list;
            }
        }
    }
}
